package Matlab;

import java.io.File;

import Global.ConstantVariable;
import Global.GlobalClass;

	public class MatlabRelatedWorksClass {
		
	public static void executeMatlabWithFile(String directoryPathOfTheMFile, String pathForTheFigureFile, String nameOfTheMFile)
	{			
		try
		{
			
			//System.out.println("fOR MATLAB directoryPathOfTheMFile:"+directoryPathOfTheMFile);
			//System.out.println("fOR MATLAB pathForTheFigureFile:"+pathForTheFigureFile);
			//System.out.println("fOR MATLAB nameOfTheMFile:"+nameOfTheMFile);
			
			if(ConstantVariable.ExecutionParameters.generateMatlabFigures==true)
			{
				File directoryFile= new File(directoryPathOfTheMFile);
				//mfile.getAbsolutePath();
				//System.out.println("Abs Path:"+mfile.getAbsolutePath());	
				//filePathOfTheMFile = mfile.getAbsolutePath();
				//System.exit(0);
				String matlabPath;

				if(GlobalClass.RunningValues.envType == ConstantVariable.Runtime_ConstantVariables.LINUX_ENV_TYPE)
					matlabPath = ConstantVariable.externalApplicationsPaths.MATHLAB_PATH_FOR_LINUX_BASED_SYSTEMS;
				else
					matlabPath = ConstantVariable.externalApplicationsPaths.MATHLAB_PATH_FOR_WINDOWS_BASED_SYSTEMS;

				Process process = Runtime.getRuntime ().exec(matlabPath + " "+nameOfTheMFile, null, directoryFile);

				if(GlobalClass.RunningValues.envType == ConstantVariable.Runtime_ConstantVariables.LINUX_ENV_TYPE)
					process.waitFor();
				else
				{
					long waitStartTimeForMatlab;

					waitStartTimeForMatlab = System.currentTimeMillis();

					String fullPathOfTheFigureFile = pathForTheFigureFile+nameOfTheMFile+ConstantVariable.Matlab_ConstantVariables.SUFFIX_OF_THE_MATLAB_FIGURE;

					File figureFile= new File(fullPathOfTheFigureFile);
					//System.out.println("YOL:"+fullPathOfTheFigureFile);

					while(System.currentTimeMillis()-waitStartTimeForMatlab < 30000)
					{
						//System.out.println("Dosya farki: "+(System.currentTimeMillis()-figureFile.lastModified()));
						if(figureFile.exists() && (System.currentTimeMillis()-figureFile.lastModified() < 3000))
						{
							//System.out.println("Dosya var ve guncel");
							break;
						}

						Thread.sleep(10);
					}
				}
			}
		}
		catch (Exception e) {			
			e.printStackTrace();
		}
	}
	
	public static void executeMatlabWithFileForVIM(String directoryPathOfTheMFile, String pathForTheFigureFile, String nameOfTheMFile)
	{			
		try
		{
			if(ConstantVariable.ExecutionParameters.generateMatlabForVIMFigures==true)
			{
				File directoryFile= new File(directoryPathOfTheMFile);
				//mfile.getAbsolutePath();
				//System.out.println("Abs Path:"+mfile.getAbsolutePath());	
				//filePathOfTheMFile = mfile.getAbsolutePath();
				//System.exit(0);
				String matlabPath;

				if(GlobalClass.RunningValues.envType == ConstantVariable.Runtime_ConstantVariables.LINUX_ENV_TYPE)
					matlabPath = ConstantVariable.externalApplicationsPaths.MATHLAB_PATH_FOR_LINUX_BASED_SYSTEMS;
				else
					matlabPath = ConstantVariable.externalApplicationsPaths.MATHLAB_PATH_FOR_WINDOWS_BASED_SYSTEMS;

				Process process = Runtime.getRuntime ().exec(matlabPath + " "+nameOfTheMFile, null, directoryFile);

				if(GlobalClass.RunningValues.envType == ConstantVariable.Runtime_ConstantVariables.LINUX_ENV_TYPE)
					process.waitFor();
				else
				{
					long waitStartTimeForMatlab;

					waitStartTimeForMatlab = System.currentTimeMillis();

					String fullPathOfTheFigureFile = pathForTheFigureFile+nameOfTheMFile+ConstantVariable.Matlab_ConstantVariables.SUFFIX_OF_THE_MATLAB_FIGURE;

					File figureFile= new File(fullPathOfTheFigureFile);
					System.out.println("YOL:"+fullPathOfTheFigureFile);

					while(System.currentTimeMillis()-waitStartTimeForMatlab < 5000)
					{
						//System.out.println("Dosya farki: "+(System.currentTimeMillis()-figureFile.lastModified()));
						if(figureFile.exists() && (System.currentTimeMillis()-figureFile.lastModified() < 1000))
						{
							//System.out.println("Dosya var ve guncel");
							break;
						}

						Thread.sleep(10);
					}
				}
			}
		}
		catch (Exception e) {			
			e.printStackTrace();
		}
	}
}
